package com.base;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.HashMap;
import java.util.List;

@Data
public class AjaxResult extends HashMap {

    public static AjaxResult success(String message){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code",200);
        ajaxResult.put("message",message);
        return ajaxResult;
    }

    public static AjaxResult success(String message, Object object){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code",200);
        ajaxResult.put("message",message);
        ajaxResult.put("data",object);
        return ajaxResult;
    }

    public static AjaxResult success(String message, List list){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code",200);
        ajaxResult.put("message",message);
        ajaxResult.put("list",list);

        return ajaxResult;
    }

    public static AjaxResult success(String message, List list,MetaInfo metaInfo){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code",200);
        ajaxResult.put("message",message);
        PageInfo pageInfo = new PageInfo(list);
        ajaxResult.put("list",list);

        metaInfo.setPage(pageInfo.getPageNum());
        metaInfo.setPages(pageInfo.getPages());
//        if(pageInfo.getTotal()%metaInfo.getSize()!=0){
//            metaInfo.setPages((int) ((pageInfo.getTotal()/metaInfo.getSize())+1));
//        }else{
//            metaInfo.setPages((int) ((pageInfo.getTotal()/metaInfo.getSize())));
//        }
        metaInfo.setTotal(pageInfo.getTotal());
        ajaxResult.put("metaInfo",metaInfo);
        return ajaxResult;
    }

    public static AjaxResult error(String message){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code",202);
        ajaxResult.put("message",message);
        return ajaxResult;
    }


    public static AjaxResult error(String message,Object object){
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("code",202);
        ajaxResult.put("message",message);
        ajaxResult.put("data",object);
        return ajaxResult;
    }



}
